package com.gx.service.impl;

import com.google.common.collect.Iterables;
import com.gx.domain.*;
import com.gx.id.DefaultIdGenerator;
import com.gx.id.IdGenerator;
import com.gx.mapper.SysRoleMapper;
import com.gx.mapper.SysRoleMenuMapper;
import com.gx.mapper.SysRoleUserMapper;
import com.gx.service.api.SysRoleMenuService;
import com.gx.service.api.SysRoleService;
import com.gx.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SysRoleServiceImpl implements SysRoleService {

    private IdGenerator idGenerator = DefaultIdGenerator.getInstance();

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysRoleUserMapper sysRoleUserMapper;

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    @Override
    public int save(SysRole sysRole) {
        sysRole.setId(idGenerator.generateId());
        sysRole.setCreateDate(new Date());
        return sysRoleMapper.insert(sysRole);
    }

    @Override
    public List<SysRole> queryAll() {
        SysRoleExample sysRoleExample = new SysRoleExample();
        return sysRoleMapper.selectByExample(sysRoleExample);
    }

    @Override
    public List<SysRole> queryByUserId(String userId) {
        return null;
    }

    @Override
    public List<SysRole> queryListLike(String name, String remark) {
        SysRoleExample sysRoleExample = new SysRoleExample();
        SysRoleExample.Criteria criteria = sysRoleExample.createCriteria();
        if (StringUtils.isNotEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (StringUtils.isNotEmpty(remark)) {
            criteria.andRemarkLike("%" + remark + "%");
        }
        return sysRoleMapper.selectByExample(sysRoleExample);
    }

    @Override
    public int batchDelete(List<Long> ids) {
        // 删除角色前优先删除用户绑定和菜单绑定（需要加事务，否则用户绑定清空菜单绑定清空，角色未删除）
        SysRoleUserExample sysRoleUserExample = new SysRoleUserExample();
        SysRoleUserExample.Criteria sysRoleUserExampleCriteria = sysRoleUserExample.createCriteria();
        sysRoleUserExampleCriteria.andRoleIdIn(ids);
        sysRoleUserMapper.deleteByExample(sysRoleUserExample);

        SysRoleMenuExample sysRoleMenuExample = new SysRoleMenuExample();
        SysRoleMenuExample.Criteria sysRoleMenuExampleCriteria = sysRoleMenuExample.createCriteria();
        sysRoleMenuExampleCriteria.andRoleIdIn(ids);
        sysRoleMenuMapper.deleteByExample(sysRoleMenuExample);

        // 删除角色
        SysRoleExample sysRoleExample = new SysRoleExample();
        SysRoleExample.Criteria criteria = sysRoleExample.createCriteria();
        criteria.andIdIn(ids);
        return sysRoleMapper.deleteByExample(sysRoleExample);
    }

    @Override
    public SysRole queryByName(String name) {
        SysRoleExample sysRoleExample = new SysRoleExample();
        SysRoleExample.Criteria criteria = sysRoleExample.createCriteria();
        criteria.andNameEqualTo(name);
        return Iterables.getFirst(sysRoleMapper.selectByExample(sysRoleExample), null);
    }

    @Override
    public int update(SysRole sysRole) {
        SysRoleExample sysRoleExample = new SysRoleExample();
        SysRoleExample.Criteria criteria = sysRoleExample.createCriteria();
        criteria.andIdEqualTo(sysRole.getId());
        return sysRoleMapper.updateByExample(sysRole, sysRoleExample);
    }

    @Override
    public int update(SysRole sysRole, List<Long> ids) {
        // 删除原本角色绑定菜单
        SysRoleMenuExample sysRoleMenuExample = new SysRoleMenuExample();
        SysRoleMenuExample.Criteria roleMenuExampleCriteria = sysRoleMenuExample.createCriteria();
        roleMenuExampleCriteria.andRoleIdEqualTo(sysRole.getId());
        sysRoleMenuMapper.deleteByExample(sysRoleMenuExample);

        // 插入新的对应关系
        SysRoleMenu sysRoleMenu = new SysRoleMenu();
        sysRoleMenu.setRoleId(sysRole.getId());
        for (Long id : ids) {
            sysRoleMenu.setMenuId(id);
            sysRoleMenuService.save(sysRoleMenu);
        }

        // 修改原角色其他信息
        return update(sysRole);
    }
}
